package cn.consult.controller.sys.park;

import cn.consult.common.api.CommonResult;
import cn.consult.sys.entity.SysDictDetail;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.service.SysDictDetailService;
import cn.consult.sys.service.SysDictService;
import cn.consult.sys.service.TPlaceBuildingService;
import cn.consult.sys.vo.SiteTree.SiteTreeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

//园区管理 > 场地管理 （楼栋）
@RestController
@Api(tags = "园区管理 > 场地楼栋")
@RequestMapping("/park/building")
public class TPlaceBuildingController {

    @Autowired
    TPlaceBuildingService tPlaceBuildingService;
    @Autowired
    SysDictDetailService sysDictDetailService;


    @ApiOperation("getTree => 拿到树状菜单")
    @GetMapping("/getTree")
    public CommonResult getSiteTree() {
        List<SiteTreeVo> treeVos = tPlaceBuildingService.tree();
        return CommonResult.success(treeVos);
    }


    @ApiOperation("getBuildingTypeAll => 获取公寓类型")
    @GetMapping("/getBuildingTypeAll")
    public CommonResult getBuildingTypeAll() {
        List<SysDictDetail> sysDictDetails = sysDictDetailService.list("buildingtype");
        return CommonResult.success(sysDictDetails);
    }


    @ApiOperation("getBuildingAll => 条件查询楼栋")
    @GetMapping("/getBuildingAll")
    public CommonResult getBuildingAll(@RequestParam Map<String, Object> params) {
        List<TPlaceBuilding> buildings = tPlaceBuildingService.selectByBuildingNameOrType(params);
        return CommonResult.success(buildings);
    }


    @ApiOperation("getBuildingById => 根据id查询楼栋信息")
    @GetMapping("/getBuildingById/{id}")
    public CommonResult getBuildingById(@PathVariable Long id) {
        TPlaceBuilding byId = tPlaceBuildingService.getById(id);
        return CommonResult.success(byId);
    }


    @ApiOperation("getBuildingByName => 楼栋名称不能一致  根据楼栋名称")
    @GetMapping("/getBuildingByName")
    public CommonResult getBuildingByName(String name) {
        boolean flag = tPlaceBuildingService.flagName(name);
        if (flag) {
            return CommonResult.success();

        }
        return CommonResult.failed("楼栋名称不能一样!");
    }


    @ApiOperation("addBuliding => 添加楼栋")
    @PostMapping("/addBuliding")
    public CommonResult addBuliding(@RequestBody TPlaceBuilding tPlaceBuilding) {
        tPlaceBuilding.setCreateTime(new Date().toString());
        boolean flag = tPlaceBuildingService.saveBuliding(tPlaceBuilding);
        return CommonResult.success(flag);
    }

    @ApiOperation("updateBuliding => 修改楼栋")
    @PutMapping("/updateBuliding")
    public CommonResult updateBuliding(@RequestBody TPlaceBuilding tPlaceBuilding) {
        tPlaceBuilding.setUpdateTime(new Date().toString());
        boolean flag = tPlaceBuildingService.updateBuliding(tPlaceBuilding);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("deleteBuliding => 删除楼栋")
    @DeleteMapping("/deleteBuliding/{bulidingId}")
    public CommonResult deleteBuliding(@PathVariable Long bulidingId) {
        boolean flag = tPlaceBuildingService.deleteBuliding(bulidingId);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
